package com.eling.elcms.travel.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eling.elcms.basedoc.model.BusinessMessage;
import com.eling.elcms.basedoc.model.PersonalInfo;
import com.eling.elcms.basedoc.model.Room;
import com.eling.elcms.basedoc.model.Room.Status;
import com.eling.elcms.basedoc.model.TargetType;
import com.eling.elcms.basedoc.model.TaskType;
import com.eling.elcms.basedoc.service.IBusinessMessageManager;
import com.eling.elcms.basedoc.service.IPersonalInfoManager;
import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.ordermanager.model.ServicePoint;
import com.eling.elcms.ordermanager.service.IServicePointManager;
import com.eling.elcms.system.model.CommonUser;
import com.eling.elcms.travel.dao.ICheckInImplementDao;
import com.eling.elcms.travel.dao.INonMemberGroupDao;
import com.eling.elcms.travel.dao.IRoomPersonalInfoDao;
import com.eling.elcms.travel.model.CheckIn;
import com.eling.elcms.travel.model.CheckIn.CheckOutStatus;
import com.eling.elcms.travel.model.CheckInCar;
import com.eling.elcms.travel.model.CheckInImplement;
import com.eling.elcms.travel.model.CheckInImplement.CheckStatus;
import com.eling.elcms.travel.model.Destination;
import com.eling.elcms.travel.model.NonMemGroupReserve;
import com.eling.elcms.travel.model.NonMemberGroup;
import com.eling.elcms.travel.model.NonMemberGroup.SignUpType;
import com.eling.elcms.travel.model.NonMemberGroup.SourceType;
import com.eling.elcms.travel.model.NonMemberRegister;
import com.eling.elcms.travel.model.RoomPersonalInfo;
import com.eling.elcms.travel.model.RoomRegister;
import com.eling.elcms.travel.model.RoomRegister.RoomStatus;
import com.eling.elcms.travel.model.SignUpRegister;
import com.eling.elcms.travel.model.SignUpRoom;
import com.eling.elcms.travel.model.SignUpRoomPrice;
import com.eling.elcms.travel.model.TravelContract;
import com.eling.elcms.travel.model.view.AdjustGroupView;
import com.eling.elcms.travel.model.view.CheckInImplementView;
import com.eling.elcms.travel.model.view.DestnAndSerPointView;
import com.eling.elcms.travel.model.view.MemberGroupView;
import com.eling.elcms.travel.model.view.NonMemberGroupView;
import com.eling.elcms.travel.model.view.SignUpRoomView;
import com.eling.elcms.travel.service.ICheckInManager;
import com.eling.elcms.travel.service.IDestinationManager;
import com.eling.elcms.travel.service.INonMemGroupReserverManager;
import com.eling.elcms.travel.service.INonMemberGroupManager;
import com.eling.elcms.travel.service.INonMemberRegisterManager;
import com.eling.elcms.travel.service.IRoomPersonalInfoManager;
import com.eling.elcms.travel.service.IRoomRegisterManager;
import com.eling.elcms.travel.service.ISignUpRoomManager;
import com.eling.elcms.travel.service.ISignUpRoomPriceManager;
import com.eling.elcms.travel.service.ITravelContractManager;

@Service
public class NonMemberGroupManagerImpl extends GenericManagerImpl<NonMemberGroup, Long>
		implements INonMemberGroupManager {

	INonMemberGroupDao nonMemGroupDao;

	@Autowired
	private INonMemberRegisterManager nonMemRegisterManager;
	@Autowired
	private IPersonalInfoManager personalInfoManager;
	@Autowired
	private IRoomPersonalInfoManager roomPersonalInfoManager;
	@Autowired
	private INonMemGroupReserverManager nonMemGroupReserveManager;
	@Autowired
	private ISignUpRoomManager signUpRoomManager;
	@Autowired
	private IRoomPersonalInfoDao roomPersonalInfoDao;
	@Autowired
	private ICheckInImplementDao checkInImplementDao;
	@Autowired
	private ISignUpRoomPriceManager signUpRoomPriceManager;
	@Autowired
	private INonMemberGroupManager nonMemGroupManager;
	@Autowired
	private ITravelContractManager travelContractManager;
	@Autowired
	private IServicePointManager serPointManager;
	@Autowired
	private IDestinationManager destnManager;
	@Autowired
	private ICheckInManager checkInManager;
	@Autowired
	private IRoomRegisterManager roomRegisterManager;
	@Autowired
 	private IBusinessMessageManager businessMessageManager;

	@Autowired
	public void setNonMemberGroupDao(INonMemberGroupDao nonMemGroupDao) {
		this.dao = nonMemGroupDao;
		this.nonMemGroupDao = nonMemGroupDao;
	}

	@Override
	public NonMemGroupReserve saveAll(NonMemberGroupView cond) {
		// 得到View里的数据
		List<SignUpRoom> rooms = cond.getSignUpRooms();
		for (SignUpRoom signUpRoom : rooms) {
			signUpRoom.setRoomStatus(CheckStatus.Pending);
		}

		NonMemberGroup group = cond.getNonMemberGroup();
		group.setSourceType(SourceType.Reservation);
		List<NonMemberRegister> signUps = cond.getSignUps();

		// 保存主表信息
		group = nonMemGroupDao.save(group);

		// 遍历散客团与报名散客关系集合
		for (NonMemberRegister register : signUps) {
			// 将散客团放入散客团与报名散客关系中
			register.setNonMemberGroup(group);
		}

		// 保存散客团与报名散客关系
		signUps = nonMemRegisterManager.save(signUps);

		// 回写散客团与报名散客关系集合
		group.setSignUps(new HashSet<NonMemberRegister>(signUps));
		group = nonMemGroupDao.save(group);

		// 保存散客团预约
		// 获取保存类型（散客，体验）
		SignUpType signUpType = cond.getNonMemberGroup().getSignUpType();
		NonMemGroupReserve reserve = new NonMemGroupReserve();
		if (signUpType.equals(SignUpType.NonMember)) {
			return reserve;
		} else {
			rooms = signUpRoomManager.save(rooms);
			NonMemGroupReserve reserveOne = new NonMemGroupReserve();
			reserveOne.setNonMemberGroup(group);
			List<NonMemGroupReserve> reserveList = nonMemGroupReserveManager.query(reserveOne);
			if (reserveList.isEmpty()) {
				reserve.setNonMemberGroup(group);
				reserve.setSignUpRooms(new HashSet<SignUpRoom>(rooms));
				reserve.setVersion(0);
			} else {
				reserve = reserveList.get(0);
				reserve.setSignUpRooms(new HashSet<SignUpRoom>(rooms));
			}
			reserve = nonMemGroupReserveManager.save(reserve);
			if(!group.getChargeDeposit()){	// 入住体验登记不收取订金则直接把状态改为有效，并推落实单
				group.setStatus(com.eling.elcms.travel.model.NonMemberGroup.Status.Effective);
				save(group);
				nonMemGroupReserveManager.sendCheckInOrder(reserve);
			}
		}
		return reserve;
	}

	@Override
	public NonMemberGroupView queryAll(NonMemberGroup cond) {
		// 得到主表信息
		NonMemberGroup group = nonMemGroupDao.get(cond.getPkNonMemberGroup());

		// 新建View
		NonMemberGroupView view = new NonMemberGroupView();
		view.setNonMemberGroup(group);
		view.setSignUps(new ArrayList<NonMemberRegister>(group.getSignUps()));

		// 按主表pk去查询散客团预约
		NonMemGroupReserve non = new NonMemGroupReserve();
		PropertyUtils.setProperty(non, "nonMemberGroup.pkNonMemberGroup", cond.getPkNonMemberGroup());
		NonMemGroupReserve memGroup = nonMemGroupReserveManager.query(non).get(0);

		// 将对应的预约房型放入View
		view.setSignUpRooms(new ArrayList<SignUpRoom>(memGroup.getSignUpRooms()));
		return view;
	}

	@Override
	public NonMemGroupReserve saveADD(NonMemberGroupView cond) {
		List<SignUpRoom> rooms = cond.getSignUpRooms();
		for(SignUpRoom signUpRoom : rooms){
			signUpRoom.setRoomStatus(CheckStatus.Pending);
		}

		NonMemberGroup group = cond.getNonMemberGroup();
		group.setSourceType(SourceType.Reservation);

		// 保存主表信息
		group = nonMemGroupDao.save(group);
		// 保存散客团预约
		// 获取保存类型（散客，体验）
		NonMemGroupReserve reserve = new NonMemGroupReserve();
			rooms = signUpRoomManager.save(rooms);
			NonMemGroupReserve reserveOne = new NonMemGroupReserve();
			reserveOne.setNonMemberGroup(group);
			List<NonMemGroupReserve> reserveList = nonMemGroupReserveManager.query(reserveOne);
			if (reserveList.isEmpty()) {
				reserve.setNonMemberGroup(group);
				reserve.setSignUpRooms(new HashSet<SignUpRoom>(rooms));
				reserve.setVersion(0);
			} else {
				reserve = reserveList.get(0);
				reserve.setSignUpRooms(new HashSet<SignUpRoom>(rooms));
			}
			reserve = nonMemGroupReserveManager.save(reserve);
			businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.NoMemDeposit,reserve.getPkNonMemGroupReserve()));

			return reserve;
		}
//	}
	
	@Override
	public Msg deleteAll(Long pkNonMemberGroup) {
		// 得到主表信息
		NonMemberGroup group = nonMemGroupDao.get(pkNonMemberGroup);
		// 查詢柱表是否關聯預約
		NonMemGroupReserve non = new NonMemGroupReserve();
		non.setNonMemberGroup(group);
		List<NonMemGroupReserve> reserves = nonMemGroupReserveManager.query(non);
		if (!reserves.isEmpty()) {
			nonMemGroupReserveManager.remove(reserves);
		}

		// 根据主表得到对应的报名信息
		Set<NonMemberRegister> set = group.getSignUps();
		// 拿到所有报名信息中的报名
		List<SignUpRegister> list = new ArrayList<SignUpRegister>();
		for (NonMemberRegister register : set) {
			list.add(register.getSignUpRegister());
		}

		// 删除中间表
		nonMemRegisterManager.remove(new ArrayList<NonMemberRegister>(set));
		// 删除主表
		nonMemGroupDao.remove(pkNonMemberGroup);
		// 删除报名
		// signUpRegisterManager.remove(list);
		return new Msg("删除成功");
	}

	@Override
	public NonMemberGroup updateStatus(NonMemberGroup cond) {
		NonMemberGroup con = get(cond.getPkNonMemberGroup());
		con.setStatus(cond.getStatus());
		NonMemberGroup nonMemberGroup = save(con);
		if(!con.getChargeDeposit() && cond.getStatus().equals(com.eling.elcms.travel.model.NonMemberGroup.Status.Confirm)){
			NonMemGroupReserve nonMemGroupReserve = new NonMemGroupReserve();
			nonMemGroupReserve.setNonMemberGroup(nonMemberGroup);
			List<NonMemGroupReserve> list = nonMemGroupReserveManager.query(nonMemGroupReserve);
			if(!list.isEmpty()){
				nonMemGroupReserve = list.get(0);
				nonMemGroupReserveManager.sendCheckInOrder(nonMemGroupReserve);
			}
		}
		return nonMemberGroup;
	}

	@Override
	public NonMemberGroup Pay(NonMemberGroup cond) {
		NonMemberGroup con = get(cond.getPkNonMemberGroup());
		con.setPledgeStatus(cond.getPledgeStatus());
		con.setChargeDate(new Date());
		con.setMoney(cond.getMoney());
		con.setStatus(NonMemberGroup.Status.Effective);
		// con.setStatus(NonMemberGroup.Status.CheckIn);
		con.setPaymentMethod(cond.getPaymentMethod());
		con.setPayDescription(cond.getPayDescription());
		con.setOperator((CommonUser) AppContext.curUser());
		con = save(con);

		// 推送入住准备落实单
		NonMemGroupReserve nmgReserve = new NonMemGroupReserve();
		nmgReserve.setNonMemberGroup(con);
		List<NonMemGroupReserve> nmgReserveList = nonMemGroupReserveManager.query(nmgReserve);
		if (!nmgReserveList.isEmpty()) {
			nmgReserve = nmgReserveList.get(0);
			nonMemGroupReserveManager.sendCheckInOrder(nmgReserve);
		}
		con= save(con);
		businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.ExperienceDeposit,con.getPkNonMemberGroup()));
		return con;
	}

	public NonMemberGroup savePay(NonMemberGroup cond) {
		NonMemberGroup con = get(cond.getPkNonMemberGroup());
		con.setPledgeStatus(cond.getPledgeStatus());
		con.setChargeDate(new Date());
		con.setPledgeMoney(cond.getPledgeMoney());
		con.setStatus(con.getStatus());
		con.setPaymentMethod(cond.getPaymentMethod());
		con.setPayDescription(cond.getPayDescription());
		con.setOperator((CommonUser) AppContext.curUser());
		con = save(con);

		// 推送入住准备落实单
		NonMemGroupReserve nmgReserve = new NonMemGroupReserve();
		nmgReserve.setNonMemberGroup(con);
		List<NonMemGroupReserve> nmgReserveList = nonMemGroupReserveManager.query(nmgReserve);
		if (!nmgReserveList.isEmpty()) {
			nmgReserve = nmgReserveList.get(0);
			nonMemGroupReserveManager.sendCheckInOrder(nmgReserve);
		}
		return save(con);
	}

	/**
	 * 散客入住登记
	 */
	@Override
	public CheckInImplement checkInSave(CheckInImplementView cond, CheckInImplement checkInImplement) {

		List<SignUpRoomView> signUpRoomList = cond.getSignUpRoomViewList();
		Map<Long, Double> checkInPricePriceMap = new HashMap<Long, Double>();

		List<SignUpRoom> srList = new ArrayList<SignUpRoom>();
		for (int i = 0; signUpRoomList!=null&&i < signUpRoomList.size(); i++) {
			SignUpRoomView surv = signUpRoomList.get(i);
			SignUpRoom sur = surv.getSignUpRoom();

			if (sur.getPkSignUpRoom() != null) {
				SignUpRoomPrice deleteSRP = new SignUpRoomPrice();
				deleteSRP.setSignUpRoom(sur);
				signUpRoomPriceManager.remove(deleteSRP);
			}

			List<SignUpRoomPrice> surpList = surv.getSignUpRoomPriceList();
			surpList = signUpRoomPriceManager.save(surpList);

			Double checkInPrice = 0.0;
			for (SignUpRoomPrice iSignRoomPrice : surpList) {
				checkInPrice = checkInPrice + iSignRoomPrice.getCheckInPrice();
			}
			checkInPricePriceMap.put(sur.getRoomType().getPkRoomType(), checkInPrice);

			sur.setSignUpRoomPrices(new HashSet<SignUpRoomPrice>(surpList));
			sur.setRoomStatus(CheckStatus.Pending);
			srList.add(sur);
		}
		srList = signUpRoomManager.save(srList);

		NonMemGroupReserve inmgr = checkInImplement.getNonMemGroupReserve();
		inmgr.setSignUpRooms(new HashSet<SignUpRoom>(srList));
		NonMemberGroup nowNmg = cond.getNonMemGroupReserve().getNonMemberGroup();
		// 拷贝NonMemberGroup对象
		NonMemberGroup target = null;
		if (nowNmg.getPkNonMemberGroup() != null) {
			target = nonMemGroupManager.get(nowNmg.getPkNonMemberGroup());
			BeanUtils.copyProperties(nowNmg, target, getNullPropertyNames(nowNmg));
		} else {
			target = inmgr.getNonMemberGroup();
			BeanUtils.copyProperties(nowNmg, target, getNullPropertyNames(nowNmg));
		}

		if (target.getPledgeStatus() == null) {
			target.setPledgeStatus(false);
		}

		inmgr.setNonMemberGroup(target);
		checkInImplement.setNonMemGroupReserve(inmgr);
		checkInImplement = checkInImplementDao.save(checkInImplement);

		if(target.getPledge() != null){
			businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.NonMemberPledge,checkInImplement.getNonMemGroupReserve().getNonMemberGroup().getPkNonMemberGroup()));
		}

		List<RoomPersonalInfo> rpiList = new ArrayList<>();

		Boolean flag = true; // 判断是否推送查房checkin
		
		Map<Long,RoomRegister> roomRegisterMap = new HashMap<Long,RoomRegister>();
		
		for (int i = 0; cond.getRoomPersonalInfoList()!=null&&i < cond.getRoomPersonalInfoList().size(); i++) {
			RoomPersonalInfo iRpi = cond.getRoomPersonalInfoList().get(i);
			iRpi.setCheckInImplement(checkInImplement);
			RoomRegister register = iRpi.getRoomRegister();
			
			if (iRpi.getCheckIn()) {
				register.getRoom().setStatus(Status.CheckIn);

				// 判断团是否含水电费
				if(inmgr.getNonMemberGroup().getIsWaterElectric()!=null && !inmgr.getNonMemberGroup().getIsWaterElectric()){
					register.setWaterElectric(false);
					register.setRoomStatus(RoomStatus.NoCheckIn);
				}else{
					register.setWaterElectric(true);
					register.setRoomStatus(RoomStatus.finish);
				}
			}
			// 保存退住价
			register.setCheckOutPrice(checkInPricePriceMap.get(iRpi.getRoomRegister().getRoom().getRoomType().getPkRoomType()));
			register.setCheckInPrice(checkInPricePriceMap.get(iRpi.getRoomRegister().getRoom().getRoomType().getPkRoomType()));
			
			if (iRpi.getNonMemberRegister().getSignUpRegister().getTravelContract() != null) {
				Long pkTravelContract = iRpi.getNonMemberRegister().getSignUpRegister().getTravelContract()
						.getPkTravelContract();
				TravelContract tc = travelContractManager.get(pkTravelContract);
				iRpi.getNonMemberRegister().getSignUpRegister().setTravelContract(tc);
			}

			iRpi.getNonMemberRegister().setNonMemberGroup(checkInImplement.getNonMemGroupReserve().getNonMemberGroup());
			
			
			if(!roomRegisterMap.containsKey(register.getRoom().getPkRoom())){
				register = roomRegisterManager.save(register);
				roomRegisterMap.put(register.getRoom().getPkRoom(), register);
			}else{
				register = roomRegisterMap.get(register.getRoom().getPkRoom());
			}
			
			iRpi.setRoomRegister(register);
			
			rpiList.add(iRpi);
			if (!iRpi.getCheckIn()) {
				flag = false;
			}
		}
		if(rpiList.get(0).getNonMemberRegister().getSignUpRegister().getPersonalInfo().getPkPersonalInfo() == null){
			for (RoomPersonalInfo roomPersonalInfo : rpiList) {
				PersonalInfo personalInfo = roomPersonalInfoManager.queryById(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getIdNumber());
				if(personalInfo!=null){
					personalInfo.setName(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getName());
					personalInfo.setSex(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getSex());
					personalInfo.setIdNumber(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getIdNumber());
					personalInfo = personalInfoManager.save(personalInfo);
					roomPersonalInfo.getNonMemberRegister().getSignUpRegister().setPersonalInfo(personalInfo);
					roomPersonalInfo.setPersonalInfo(personalInfo);
				}
			}
			rpiList = roomPersonalInfoDao.save(rpiList);
		}else{
			for (RoomPersonalInfo roomPersonalInfo : rpiList) {
				/*if(roomPersonalInfo.getPkRoomPersonalInfo() == null){
					PersonalInfo personalInfo = roomPersonalInfoManager.queryById(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getIdNumber());
					personalInfo.setName(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getName());
					personalInfo.setSex(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getSex());
					personalInfo.setIdNumber(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getIdNumber());
					personalInfo = personalInfoManager.save(personalInfo);
					roomPersonalInfo.getNonMemberRegister().getSignUpRegister().setPersonalInfo(personalInfo);
					roomPersonalInfo.setPersonalInfo(personalInfo);
				}*/
				roomPersonalInfoDao.saveBySql(roomPersonalInfo);
			}
		}

		if (flag) { // 都已登记则表示都入住了,此时推送入住数据,一个房间一条退房数据
			Map<Long, Room> roomMap = new HashMap<>();
			for (RoomPersonalInfo iRpi : rpiList) {
				if(roomMap.containsKey(iRpi.getRoomRegister().getRoom().getPkRoom())){
					continue;
				}else{
					roomMap.put(iRpi.getRoomRegister().getRoom().getPkRoom(), iRpi.getRoomRegister().getRoom());
					CheckInImplement cii = iRpi.getCheckInImplement();
					CheckIn checkIn = new CheckIn();
					checkIn.setRoomRegister(iRpi.getRoomRegister());
					checkIn.setCheckInImplement(cii);
					checkIn.setCheckInDate(cii.getNonMemGroupReserve().getNonMemberGroup().getCheckInDate());
					checkIn.setCheckOutDate(cii.getNonMemGroupReserve().getNonMemberGroup().getCheckOutDate());
					checkIn.setRegisterDate(new Date());
					checkIn.setOperator((CommonUser) AppContext.curUser());
					checkIn.setServicePoint(cii.getServicePoint());
					checkIn.setStatus(com.eling.elcms.travel.model.CheckIn.Status.CheckIn);
					checkIn.setCheckOutStatus(CheckOutStatus.NoCheckOut);
					checkInManager.save(checkIn);
				}
			}
			// 所有人已入住则更新团状态为入住
			target = checkInImplement.getNonMemGroupReserve().getNonMemberGroup();
			target.setStatus(com.eling.elcms.travel.model.NonMemberGroup.Status.CheckIn);
			nonMemGroupManager.save(target);
			businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.CheckOut,target.getPkNonMemberGroup()));
		}
		Set<RoomPersonalInfo> saveRpiSet = checkInImplement.getRoomPersonalInfos();
		if(saveRpiSet != null){
			saveRpiSet.clear();
			saveRpiSet.addAll(rpiList);
			checkInImplement.setRoomPersonalInfos(saveRpiSet);
		}else{
			checkInImplement.setRoomPersonalInfos(new HashSet<>(rpiList));
		}
		return checkInImplementDao.save(checkInImplement);
	}

	public static String[] getNullPropertyNames(Object source) {
		final BeanWrapper src = new BeanWrapperImpl(source);
		java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

		Set<String> emptyNames = new HashSet<String>();
		for (java.beans.PropertyDescriptor pd : pds) {
			Object srcValue = src.getPropertyValue(pd.getName());
			if (srcValue == null)
				emptyNames.add(pd.getName());
		}
		String[] result = new String[emptyNames.size()];
		return emptyNames.toArray(result);
	}

	@Override
	public List<MemberGroupView> queryFit(Long pkServicePoint, Boolean checkIn, Boolean fstatus, Date startDate,
			Date endDate) {

		return nonMemGroupDao.queryFit(pkServicePoint, checkIn, fstatus, startDate, endDate);// Boolean
																								// fstatus,
	}

	@Override
	public List<DestnAndSerPointView> queryDestnAndSerPoint() {
		List<DestnAndSerPointView> returnList = new ArrayList<>();
		List<ServicePoint> serPointList = serPointManager.operableServicePoints();
		List<Destination> destnList = destnManager.getAll();
		for (Destination destn : destnList) {
			DestnAndSerPointView view = new DestnAndSerPointView();
			view.setPkDestnAndSerp(destn.getPkDestination());
			view.setName(destn.getName());
			view.setIsSerPoint(false);
			view.setStrKey(destn.getPkDestination() + "false");
			returnList.add(view);
		}
		for (ServicePoint serPoint : serPointList) {
			DestnAndSerPointView view = new DestnAndSerPointView();
			view.setPkDestnAndSerp(serPoint.getPkServicePoint());
			view.setName(serPoint.getName());
			view.setIsSerPoint(true);
			view.setStrKey(serPoint.getPkServicePoint() + "true");
			returnList.add(view);
		}
		return returnList;
	}

	@Override
	public List<NonMemGroupReserve> isverifyquery(NonMemGroupReserve cond) {
		List<NonMemGroupReserve> reserveList = new ArrayList<>();
		List<NonMemGroupReserve> query = nonMemGroupReserveManager.query(cond);
		List<NonMemberGroup> groupList = new ArrayList<>();
		for (NonMemGroupReserve reserve : query) {
			NonMemberGroup nonMemberGroup = reserve.getNonMemberGroup();
			groupList.add(nonMemberGroup);
		}
		for (int i = 0; i < groupList.size(); i++) {
			// 如果他收费了 那么他的状态就是需要收取订金
			if (groupList.get(i).getMoney() != null && groupList.get(i).getMoney() > 0) {
				if (groupList.get(i).getChargeDeposit() == null || groupList.get(i).getChargeDeposit() == false) {
					groupList.get(i).setChargeDeposit(true);
					nonMemGroupDao.save(groupList.get(i));
				}
			}
		}
		query = nonMemGroupReserveManager.query(cond);
		for (int i = 0; i < query.size(); i++) {
			NonMemGroupReserve nonMemGroupReserve = query.get(i);
			// 只查需要收取订金的
			if (nonMemGroupReserve.getNonMemberGroup() != null) {
				if (nonMemGroupReserve.getNonMemberGroup().getChargeDeposit() != null
						&& nonMemGroupReserve.getNonMemberGroup().getChargeDeposit()) {
					reserveList.add(nonMemGroupReserve);
				}
			}
		}
		return reserveList;
	}

	@Override
	public NonMemberGroup nonmemSaveAdjust(AdjustGroupView adjustView) {
		NonMemberGroup nonMemberGroup = adjustView.getNonMemberGroup();
		// TODO因为页面未传递此此参数，暂时写死了（且此服务只用于预约散客）
		nonMemberGroup.setSourceType(SourceType.Reservation);
		nonMemberGroup = save(nonMemberGroup);
		List<SignUpRoom> signUpRoomList = adjustView.getSignUpRoomList();
		signUpRoomList = signUpRoomManager.save(signUpRoomList);
		NonMemGroupReserve nonMemGroupReserve = new NonMemGroupReserve();
		nonMemGroupReserve.setNonMemberGroup(nonMemberGroup);
		List<NonMemGroupReserve> reserveList = nonMemGroupReserveManager.query(nonMemGroupReserve);
		if(!reserveList.isEmpty()){
			nonMemGroupReserve = reserveList.get(0);
			nonMemGroupReserve.setSignUpRooms(new HashSet<>(signUpRoomList));
			nonMemGroupReserve = nonMemGroupReserveManager.save(nonMemGroupReserve);
			CheckInImplement checkInImplement = new CheckInImplement();
			checkInImplement.setNonMemGroupReserve(nonMemGroupReserve);
			List<CheckInImplement> ciiList = checkInImplementDao.query(checkInImplement);
			if(!ciiList.isEmpty()){
				checkInImplement = ciiList.get(0);
				if(checkInImplement.getStatus().equals(CheckStatus.Confirmed)){
					// 散客团的落实单状态为已落实时还原状态为准备中
					checkInImplement.setRoomStatus(CheckStatus.Pending);
					
//					if(checkInImplement.getCheckInCar() != null 
//							&& checkInImplement.getCheckInCar().getStatus().equals(CheckStatus.Pended)){
//						checkInImplement.getCheckInCar().setStatus(CheckStatus.Pending);
//					}
					Set<CheckInCar> checkInCar = checkInImplement.getCheckInCar();
					if(checkInCar!=null && !checkInCar.isEmpty()){
						for (CheckInCar car : checkInCar) {
							if(car.getStatus().equals(CheckStatus.Pended)){
								car.setStatus(CheckStatus.Pending);
							}
						}
					}
					
					if(checkInImplement.getCheckInDinner() != null 
							&& checkInImplement.getCheckInDinner().getStatus().equals(CheckStatus.Pended)){
						checkInImplement.getCheckInDinner().setStatus(CheckStatus.Pending);
					}
					checkInImplement.setStatus(CheckStatus.Doing);
					for(SignUpRoom sur : signUpRoomList){
						sur.setRoomStatus(CheckStatus.Pending);
					}
					signUpRoomManager.save(signUpRoomList);
					checkInImplementDao.save(checkInImplement);
				}
			}
		}
		return nonMemberGroup;
	}
}
